﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace test5_3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public class Student     			//这是一个基类
        {
            protected string name;          //基类的数据成员
            protected int age;
            public Student(string name, int age)  //基类带参构造函数
            {
                this.name = name;
                this.age = age;
            }
            public string Study()            //基类的方法
            {
                return string.Format("Student({0}):我今年{1}岁，我正在学习！", name, age);
            }
        }


        public class Undergraduate : Student   	//这是一个派生类
        {
            private string subject;              //派生类数据成员
            public Undergraduate()
                : base("无名", 0)          //派生类的默认构造函数
            {
                subject = "未知";
            }
            public Undergraduate(string name, int age, string subject)
                : base(name, age)  //派生类的带参构造函数         
            {
                this.subject = subject;
            }
            public string GetMessage()        //派生类方法
            {
                return string.Format("Undergraduate({0}):我今年{1}岁，我的专业是{2}！", name, age, subject);
            }
            //public override string Study()            //派生类覆盖基类方法
            //{
            //    return string.Format("Undergraduate({0}):我今年{1}岁，我正在学习{2}专业！", name,age,subject);
            //}
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Undergraduate u;
            if (TextName.Text == "") u = new Undergraduate();
            else
            {
                u = new Undergraduate(TextName.Text, Convert.ToInt32(TextAge.Text), TextSubject.Text);
            }
            labelshow.Text = u.GetMessage();
            labelshow.Text += "\n\n" + u.Study();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "胡明辉541901060108";
        }
    }

}
